<?php
class longController extends appController
{
    public function index()
    {
        if(!isset($_SESSION['openid']))
        {
            $res = $this->getOpenidByShake();
    	    if($res['errcode']==0&&$res['data']['openid'])
    	    {
    	        $count = Model('long')->is_save_openid($res['data']['openid']);
                if($count==0){
    	            $info['openid'] = $res['data']['openid'];
    	            Model('long') -> insert($info,"long_users");
    	        }    	        
    	            
    	        $_SESSION['openid'] = $res['data']['openid'];
    	    }
//     	    else
//     	    {
// //     	        echo("非法操作！errmsg：".$res['errmsg']);exit();
//     	        echo("非法操作!请通过摇一摇进入该页面");exit();
//     	    }
        }
        $this->assign("ticket", $_REQUEST['ticket']);
        $this->assign_global();
    
//         $jsdk = Model('long')->get_jsdk_long();
    
//         $this->assign("jsdk", $jsdk);
    
        $this->display("index.html");
    
    }
    
    /*
     * @author 
     * @desc 摇一摇时触发，异步返回中奖信息
     * res.data.status 0=>中奖  1=>谢谢参与   2=>非法操作
     */
    public function ajaxPrize()
    {
         
        $res = array(
            "error"=>0,
            "content"=>"",
            data=>array()
             
        );
        if(!$_SESSION['openid'])
        {
            $res['content'] = "刷新后重新摇一摇!";
            $res['error'] = 2;
        }
        else
        {
             
            $card_type = $_REQUEST['card_type']?$_REQUEST['card_type']:0;
            $prize = Model("long")->getPrize2($_SESSION['openid'],$card_type);
             
            if($prize['user_id'] == 0){
                 
                $res['error'] = 1;
                $res['content'] = "谢谢参与!";
                 
            }else{
                //中奖
//                 $cards = explode(",",$prize['card_id']);
//                 if(is_array($cards))
//                 {
//                     foreach ($cards as $k => $v)
//                     {
//                         $card_list[]['cardId'] = $v;
//                     }
//                 }
//                 else
//                 {
//                     $card_list[]['cardId'] = $prize['card_id'];
//                 }
                
//                 $jsdk_card = Model('long')->build_jsdk_card($card_list);
//                 $res['cards'] = $jsdk_card;
                $res['is_saved'] = Model('long') -> isSaved($_SESSION['openid'])?"true":"false";
                $res['data'] = $prize;
            }
             
        }
        die(json_encode($res));
         
    }
    
    public function getCards()
    {
        $res = array("error"=>1,"content"=>"刷新后重新摇一摇!");
        if(!$_SESSION['openid'])
        {
            $res['content'] = "刷新后重新摇一摇!";
            $res['error'] = 1;
        }
        $m = Model('long');
        if(!$m -> isSaved($_SESSION['openid'])){
            $info['telphone'] = yf_empty($_POST['tel'],"请输入手机号码");
            if(hy_check($info['telphone'],'m')=="#@_error")
            {
                $res['content'] = "请输入合理的手机号码";
                $this->showMsg($res);
            }
            $result = $m->updateInfo($info,array("table"=>"long_users","openid"=>$_SESSION['openid']));
            if(!$result){
                $this->showMsg($res);
            }
        }
        
         $card = $m -> getCard($_SESSION['openid']);
        if(!$card){
            $res['content'] = "当前无卡券可领取";
            $this->showMsg($res);
        }else{
            $res['error'] = 0;
            $res['content'] = "领取卡券中";
            $res['cardlist'] = $card['jsdk_card'];
            $res['user_id'] = $card['id'];
        }
        $this->showMsg($res);
        
    }
    
    // 领取卡券后异步回调
    public function saveCard()
    {
        $id = $_REQUEST['id']?$_REQUEST['id']:0;
    
        if($id == 0)
        {
            die("error".$id);
        }
        Model('long')->saveCard($id);
        echo "success".$id;
    }
    
    
        
    /**
	 * 摇一摇周边获取openid以及设备信息
	 */
	public function getOpenidByShake()
	{
	    
	    $access_token = Model('long')->getAccessTokenLong();
	    $data['ticket'] = $_GET['ticket'];
	    
	    $url = "https://api.weixin.qq.com/shakearound/user/getshakeinfo?access_token=$access_token";
	    $json = $this->post_it_long($url, json_encode($data));//获取微信用户基本信息
	    $arr = json_decode($json,true);
	    return $arr;
	}
    
	/**
	 * 用post方法获取数据
	 * @param $url
	 * @param $data
	 * @return
	 */
	public function post_it_long($url, $data)
	{
	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_URL, $url);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
	    curl_setopt($curl, CURLOPT_POST, 1);
	    curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
	    $result = curl_exec($curl);
	    if($result){
	        curl_close($curl);
	        return $result;
	    }
	    else {
	        $error = curl_errno($curl);
	        curl_close($curl);
	        return false;
	    }
	}
	
}